<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>教师增加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="__ADMIN__/lib/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="__ADMIN__/css/font.css">
    <link rel="stylesheet" href="__ADMIN__/css/xadmin.css">
    <link rel="stylesheet" href="__ADMIN__/css/div.css">
    <script type="text/javascript" src="__ADMIN__/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="text">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>ID</label>
                    <div class="layui-input-inline">
                        <input type="text" id="id" name="id" required="" lay-verify="required|number|idcheck"
                               autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为用户唯一标识
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" required="" lay-verify="required|expertname"
                               autocomplete="off"
                               class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>密码</label>
                    <div class="layui-input-inline">
                        <input type="password" id="pwd" name="pwd" required="" lay-verify="required|pass"
                               autocomplete="off" class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>性别</label>
                    <div class="layui-input-inline">
                        <select id="sex" name="sex" lay-verify="required" autocomplete="off" class="layui-input">
                            <option value=""></option>
                            {volist name="sexlist" id="vo"}
                            <option value="{$vo.sex}">{$vo.sex}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" id="email" name="email" required="" lay-verify="email" autocomplete="off"
                               class="layui-input"></div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>年龄</label>
                    <div class="layui-input-inline">
                        <input type="text" id="age" name="age" required="" lay-verify="required|agecheck"
                               autocomplete="off" class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>级别</label>
                    <div class="layui-input-inline">
                        <select id="level" name="level" lay-verify="required" autocomplete="off" class="layui-input">
                            <option value=""></option>
                            {volist name="levellist" id="vo"}
                            <option value="{$vo.level}">{$vo.level}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>地址</label>
                    <div class="layui-input-inline">
                        <input type="text" id="address" name="address" required="" lay-verify="required"
                               autocomplete="off" class="layui-input"></div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>手机</label>
                    <div class="layui-input-inline">
                        <input type="text" id="phone" name="phone" required="" lay-verify="required|phone"
                               autocomplete="off" class="layui-input"></div>
                </div>

                <hr class="hr20">

                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>个人简介</label>
                    <div class="layui-input-inline">
                    <textarea rows="10" cols="30" class="tarea" id="person" name="person" required=""
                              lay-verify="required"
                              autocomplete="off" style="width: 640px; height: 100px;margin: 0;"></textarea>
                    </div>
                    <hr class="hr20">
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">增加</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>layui.use(['form', 'layer', 'jquery'], function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        //自定义验证规则
        form.verify({
            idcheck: function (value) {
                value = $('#id').val();
                //console.log(value);
                if (!(((value.length) > 0) && ((value.length <= 10)))) {
                    return '请输入正确的ID!';
                }
                if (verifyStudentIsExist()) {
                    return 'ID已存在，请修改ID！';
                }
            },
            expertname: [/^[\u4e00-\u9fa5]{2,4}$|^[a-zA-Z]{1,30}$/, '姓名只能为2至4个汉字或1至30个英文单词'],
            pass: [/^[a-zA-Z]\w{5,17}$/, '以字母开头，长度在6~18之间，只能包含字母、数字和下划线'],
            agecheck: [/(^[2-7][0-9]$)/, '只能输入20到79之间任意数字'],
        });

        function verifyStudentIsExist() {
            var expertid = $('#id').val();
            var resCode;
            $.ajax({
                url: "../Admin/expertidcheck",
                type: 'POST',
                dataType: "json",
                data: {"id": expertid},
                async: false,//必须是同步提交的
                success: function (res) {
                    // console.log(res.data);
                    if (res.data == 1) {
                        //  console.log(res.data);
                        resCode = 1;//数据库中有该用户数据！
                    } else if (res.data == 0) {
                        //console.log(res.data);
                        resCode = 0;//数据库中没有该用户数据！
                    }
                },
            });
            if (resCode == 1) {
                return true;
            } else if (resCode == 0) {
                return false;
            }
        }

        //监听提交
        form.on('submit(add)', function (data) {
            //console.log(data.field);
            //发异步，把数据提交给php
            var id = data.field.id;
            var name = data.field.name;
            var pwd = data.field.pwd;
            var sex = data.field.sex;
            var mail = data.field.email;
            var age = data.field.age;
            var level = data.field.level;
            var address = data.field.address;
            var phone = data.field.phone;
            var person = data.field.person;
            //console.log(data);
            $.ajax({
                url: "../Admin/expertinsert",
                type: "post",
                dataType: "json",
                data: {
                    "id": id, "name": name, "pwd": pwd, "sex": sex, "mail": mail, "age": age,
                    "level": level, "address": address, "phone": phone, "person": person
                },
                async: false,//必须是同步提交的
                success: function (t) {
                },
            });
            layer.alert("增加成功", {
                    icon: 6
                },
                function () {
                    //关闭当前frame
                    xadmin.close();
                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                });
            return false;
        });
    });
</script>
</body>
</html>